package com.khyr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.entity.Role;
import com.khyr.service.RoleService;
import com.khyr.vo.R;
import com.khyr.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@RestController
@RequestMapping("/systemManager/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //查询所有的角色信息
    @GetMapping("/selectAllRole")
    public R<List<Role>> selectAllRole(){

        return roleService.selectAllRole();
    }

    // 根据用户ID查询用户拥有的角色IDS
    @GetMapping("/findRolesByUserId/{userId}")
    public R<Role> findRolesByUserId(@PathVariable Long userId){
        return roleService.findRolesByUserId(userId);
    }

    //页面刷新时查询所有
    @PostMapping("/findRoleByCond")
    public R<Page<Role>> findRoleByCond(@RequestBody RoleVo roleVo){
        return roleService.findRoleByCond(roleVo);
    }

    //添加
    @PostMapping("/addRole")
    public R<Integer> addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    //修改
    @PostMapping("/updateRole")
    public R<Integer> updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }


    //删除
    @DeleteMapping("/deleteRoleByRoleIds/{ids}")
    public R<Integer> deleteRoleByRoleIds(@PathVariable List<Long> ids){
        return roleService.deleteRoleByRoleIds(ids);
    }

    //查询一个
    @PostMapping("/getRoleByRoleId/{id}")
    public R<Role> getRoleByRoleId(@PathVariable Long id){
        return roleService.getRoleByRoleId(id);
    }

    //保存用户和角色之间的关系
    @PostMapping("/saveRoleUser/{userId}")
    public R<Integer> saveRoleUser(@PathVariable Long userId,@RequestBody List<Long> roleIds){
        return roleService.saveRoleUser(userId, roleIds );
    }

    //保存角色和菜单之间的关系
    @PostMapping("/saveRoleMenu/{roleId}")
    public R<Integer> saveRoleMenu(@PathVariable Long roleId,@RequestBody List<Long> menuIds){
        return roleService.saveRoleMenu(roleId, menuIds);
    }
}
